package modules::cuffdiff;
use strict;

sub new {
  my ($class,$config)    = @_;

  my $self = {
               'util' => $config,
             };
  bless $self,$class;
  return $self;
}

sub prepare {
  my $self  = shift;
  my $util                  = $self->{'util'};
  my $job_name              = $util->{'_job_name'};
  my $path                  = $util->{'_path'};
  my $cuffdiff_dir          = $util->{'_cuffdiff_dir' };

  print "Creating '$cuffdiff_dir'...\n"; 
  mkdir  $cuffdiff_dir, 777; chmod(0777, $cuffdiff_dir);
  
  return;
}

sub run {
  my $self                  = shift;
  my $util                  = $self->{'util'};
  my $cuffdiff_dir          = $util->{'_cuffdiff_dir' };
  my $genome                = $util->{'_genome'       };
  my $tophat_dir            = $util->{'_tophat_dir'   };
  my $samples               = $util->{'_samples'      };
  my $gtf                   = $util->{'_gtf_file'     };
  my $cond                  = $util->{'_cond'         };

  my ($cmd,$conditions); 
  my $i=0;
  # keys are labels, values are libraries
  
  my ($cmd_lib,$cmd_label,$cases);
  
  foreach my $smpls (@$samples) {
    my @smpls_path;
    map {push @smpls_path,"$tophat_dir/$_/accepted_hits.bam"} @$smpls;
    push @$cases,join (',',@smpls_path);
  } 
  map {$cmd_lib .= "$_ "} @$cases;
  $cmd_label = join (',',@$cond);

  $cmd = qq(bsub -J cuffdiff.glob2 -n 8 -R "span[ptile=8] rusage[mem=5000]" )
       . qq(" /usr/users/ga002/corpasm/workarea/bin/cuffdiff -o $cuffdiff_dir)
       . qq( -b $genome.fa -p 8 )
       . qq( -L $cmd_label -u $gtf )
       . qq( $cmd_lib");
  print "\n\nRUNNING COMMAND:\n$cmd\n\n";
  


  #warn Dumper $conditions;
  system($cmd);
  my $utility = modules::utilities->new();
  $utility->check_completion();
  return;

}
1; 


